export function processStream(data: string): string {
  const result: any = data
    .split('\n')
    .filter(line => line?.startsWith('data: '))
    .filter(line => line !== 'data: [DONE]')
    .map(line => {
      try {
        return JSON.parse(line?.substring(6))
      } catch (err) {
        return {}
      }
    })

  return contextToString(result)
}

export function contextToString(data: any[]): string {
  return data
    .map((item: { choices: { delta: { content: string } }[] }) =>
      item?.choices
        ?.map(
          (choice: { delta: { content: string } }) => choice?.delta?.content
        )
        ?.join('')
    )
    ?.join('')
}
